package com.zyf.cloud.getway.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.zyf.cloud.getway.data.ResponseDto;
import com.zyf.cloud.getway.data.StatusCodeConstants;
import com.zyf.cloud.getway.feign.UserFeign;
import com.zyf.cloud.getway.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/token")
public class LoginController {

    @Autowired(required = false)
    private UserFeign userFeign;

    private ResponseDto responseDto;

    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 登录方法
     * @param name
     * @param password
     * @return
     */
    @PostMapping(value = "/login")
    public String login(@RequestParam("name") String name, @RequestParam("password") String password){
        //账号密码校验
        if(StrUtil.isNotEmpty(name) && StrUtil.isNotEmpty(password)){
            //通过feign调用user-service查询登录的服务
            String result = userFeign.login(name,password);
            if (result != null ){
                ResponseDto responseData = JSON.parseObject(result,ResponseDto.class);
                if (responseData.getCode().equals(StatusCodeConstants.SUCCESS)){
                   Map<String, String> map = tokenUtil.getToken(name, "1");
                    //返回结果
                    responseDto = new ResponseDto(StatusCodeConstants.SUCCESS,"成功并返回数据", map.get("token"), map.get("refreshToken"),responseData.getData());
                }else {
                    responseDto = new ResponseDto(responseData.getCode(),responseData.getMessage(),null);
                }
            }else {
                responseDto = new ResponseDto(StatusCodeConstants.PHONE_OR_PASSWORD_ERROR,"手机号或密码错误",null);
            }
        }else {
            responseDto = new ResponseDto(StatusCodeConstants.PHONE_OR_PASSWORD_ERROR,"手机号或密码错误",null);
        }
        return JSON.toJSONString(responseDto);
    }


    /**
     * 通过token拿用户信息
     * @param token
     * @return
     */
    @GetMapping("/get_info")
    private Object getInfoByToken(String token) {
        return "{\n" +
                "    \"name\": \"admin\",\n" +
                "    \"user_id\": 2,\n" +
                "    \"access\": [\"admin\"],\n" +
                "    \"token\": \"admin\",\n" +
                "    \"avatar\": \"https://avatars0.githubusercontent.com/u/20942571?s=460&v=4\"\n" +
                "  }";
    }

    /**
     * 刷新JWT
     * @param refreshToken
     * @return
     */
    @GetMapping("/refresh")
    public String refreshToken(@RequestParam("refreshToken") String refreshToken, @RequestParam("phone") String phone){
        responseDto = tokenUtil.refreshToken(phone, "1", refreshToken);
        return JSON.toJSONString(responseDto);
    }

    /**
     * 退出时删除key
     * @param phone
     * @return
     */
    @GetMapping("/logout")
    public String logout( @RequestParam("phone") String phone){
        tokenUtil.removeToken(phone, "1");
        responseDto = new ResponseDto(StatusCodeConstants.SUCCESS, "退出成功",null);
        return JSON.toJSONString(responseDto);
    }
}
